package com.blackjack
{
	import com.blackjack.cards.*;

	public class Dealer
	{
		private var _points:uint = 0;
		private var _deck:Deck;
		
		public function Dealer()
		{
			initDealer();
		}
		
		public function takeCard():void{
			var c:Card = Stack.instance.takeCard();
			if(!deck.deck.length) c.open = true;
			_deck.addCard(c);
			calculatePoints();
		}
		
		private function initDealer():void{
			_deck = new Deck;
		}
		
		public function get points():uint {return _points;}
		public function get deck():Deck {return _deck;}
		
		public function update():void{
			deck.update();
			var ocPosx:int = 200-deck.deck.length*40
			var ccPosx:int = 300-deck.deck.length*40
			for(var i:int = 0;i < deck.deck.length;i++){
				if(deck.deck[i].open){
					deck.deck[i].x = ocPosx+i*40;
				}else{
					deck.deck[i].x = ccPosx+i*40;
				}
				deck.deck[i].y = 30;
			}
		}
		
		private function calculatePoints():void{
			_points = 0;
			for(var i:int = 0;i < deck.deck.length;i++){
				switch(deck.deck[i].rank){
					case "2":
						_points += 2;
						break;
					case "3":
						_points += 3;
						break;
					case "4":
						_points += 4;
						break;
					case "5":
						_points += 5;
						break;
					case "6":
						_points += 6;
						break;
					case "7":
						_points += 7;
						break;
					case "8":
						_points += 8;
						break;
					case "9":
						_points += 9;
						break;
					case "10":
						_points += 10;
						break;
					case "Jack":
						_points += 10;
						break;
					case "Queen":
						_points += 10;
						break;
					case "King":
						_points += 10;
						break;
					case "Ace":
						if(_points+11 >= 21) _points += 1;
						else _points += 11;
						break;
				}
			}
		}
	}
}